package com.loong.musicdemo.util.dataDeal;

import com.loong.musicdemo.model.Comment;
import com.loong.musicdemo.util.httpUtil.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CommentDeal {
    private static  int allPostNum = 0;
    private static  int hotPostNum = 0;

    /**
     *   每次请求20条 最热的评论
     * @param id 歌单id
     * @return
     */
    public static List<Comment> getPlayListHotComments(String id){
        String url = "http://47.112.224.146:3000/comment/playlist?id="+id+"&offset="+hotPostNum*20;
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        List<Comment> hotComments = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(response);
            JSONArray hots = object.getJSONArray("hotComments");
            if (!hots.toString().equals("[]")) {
                for (int i = 0; i < hots.length() ; i++) {
                    Comment c = new Comment();
                    JSONObject comment = hots.getJSONObject(i);
                    JSONObject user = comment.getJSONObject("user");
                    c.setCommentId(comment.getString("commentId"));
                    c.setUserAvatarUrl(user.getString("avatarUrl"));
                    c.setContent(comment.getString("content"));
                    c.setUserNickName(user.getString("nickname"));
                    c.setLikeCount(comment.getString("likedCount"));
                    c.setUserId(user.getString("userId"));
                    hotComments.add(c);
                }
                hotPostNum++;
                return hotComments;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hotComments;
    }

    /**
     * 每次请求只获取20条，下一次请求在上一次的基础上获取20条
     * @param id  歌单id
     * @return 全部歌单评论
     */
    public static List<Comment> getPlayListAllComments(String id){
        String url = "http://47.112.224.146:3000/comment/playlist?id="+id+"&offset="+allPostNum*20;
        String response = HttpUtil.sendReturnOKHttpRequest(url);
        List<Comment> allComments = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(response);
            JSONArray comments = object.getJSONArray("comments");
            if(!comments.equals("[]")) {
                for (int i = 0; i < comments.length(); i++) {
                    Comment c = new Comment();
                    JSONObject comment = comments.getJSONObject(i);
                    JSONObject user = comment.getJSONObject("user");
                    c.setCommentId(comment.getString("commentId"));
                    c.setUserAvatarUrl(user.getString("avatarUrl"));
                    c.setContent(comment.getString("content"));
                    c.setUserNickName(user.getString("nickname"));
                    c.setLikeCount(comment.getString("likedCount"));
                    c.setUserId(user.getString("userId"));
                    allComments.add(c);
                }
                allPostNum++;
                return allComments;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return allComments;
    }
}
